package com.zh.hbase;

import java.io.IOException;
import java.io.InterruptedIOException;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.RetriesExhaustedWithDetailsException;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;

public class TestHbaseClient {
	
	/**
	 * 获取Hbase table
	 * @param args
	 */
	public static HTable getTable(String tbName){
		Configuration conf = HBaseConfiguration.create();
		HTable table = null;
		try {
			table = new HTable(conf, tbName);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return table;
	}
	
	
	/**
	 * get 'hadoop19：tb1','1001','info:name'
	 * @param args
	 */
	public static void getData(HTable table){
		Get get = new Get(Bytes.toBytes("1002"));
		get.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"));
		get.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"));
		get.addColumn(Bytes.toBytes("info"), Bytes.toBytes("sex"));
		try {
			Result rs = table.get(get);
			for(Cell cell:rs.rawCells()){
				System.out.println(Bytes.toString(CellUtil.cloneFamily(cell))+"  "
						+Bytes.toString(CellUtil.cloneQualifier(cell)) +"  "
								+ Bytes.toString(CellUtil.cloneValue(cell))+"    "
								+ cell.getTimestamp()
						);
			
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	
	/**
	 * put
	 * @param args
	 */
	public static void putData(HTable table){
		Put put =  new Put(Bytes.toBytes("1003"));
		put.add(Bytes.toBytes("info"),Bytes.toBytes("age"),Bytes.toBytes("11e"));
		put.add(Bytes.toBytes("info"),Bytes.toBytes("name"),Bytes.toBytes("jeack"));
		put.add(Bytes.toBytes("info"),Bytes.toBytes("sex"),Bytes.toBytes("maele"));
		
		try {
			table.put(put);
		} catch (RetriesExhaustedWithDetailsException e) {
			e.printStackTrace();
		} catch (InterruptedIOException e) {
			e.printStackTrace();
		}
		getData(table);
	}
	
	
	/**
	 * delete
	 * @param args
	 */
	
	public static void deleteData(HTable table){
		Delete del = new Delete(Bytes.toBytes("1002"));
		del.deleteColumn(Bytes.toBytes("info"), Bytes.toBytes("age"));
		try {
			table.delete(del);
		} catch (IOException e) {
			e.printStackTrace();
		}
		getData(table);
	}
	
	/**
	 * scan
	 * @param args
	 */
	
	public static void scanData(HTable table){
		Scan scan = new Scan();
		try {
			ResultScanner resultScan =	table.getScanner(scan);
			for(Result rs: resultScan){
				System.out.println("rowKey:"+Bytes.toString(rs.getRow()));
				System.out.println("列簇        列         列值          时间戳");
				for(Cell cell:rs.rawCells()){
					System.out.println(Bytes.toString(CellUtil.cloneFamily(cell))+"  "
							+Bytes.toString(CellUtil.cloneQualifier(cell)) +"  "
									+ Bytes.toString(CellUtil.cloneValue(cell))+"    "
									+ cell.getTimestamp()
							);
				
				}
			}
			
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * range
	 * @param args
	 */
	
	public static void rangeData(HTable table,String startRow,String stopRow){
		Scan scan = new Scan();
		
		if(StringUtils.isNotBlank(startRow)){
			scan.setStartRow(Bytes.toBytes(startRow));
		}
		if(StringUtils.isNotBlank(stopRow)){
			scan.setStopRow(Bytes.toBytes(stopRow));
		}
		
		try {
			ResultScanner resultScan =	table.getScanner(scan);
			for(Result rs: resultScan){
				System.out.println("rowKey:"+Bytes.toString(rs.getRow()));
				System.out.println("列簇        列         列值          时间戳");
				for(Cell cell:rs.rawCells()){
					System.out.println(Bytes.toString(CellUtil.cloneFamily(cell))+"  "
							+Bytes.toString(CellUtil.cloneQualifier(cell)) +"  "
									+ Bytes.toString(CellUtil.cloneValue(cell))+"    "
									+ cell.getTimestamp()
							);
				
				}
			}
			
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	public static void main(String[] args) {
			HTable table = getTable("hadoop19:tb1");	
			putData(table);
			rangeData(table,"1001",null);
//			getData(table);
	}

}
